﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;

namespace XGame
{
    // /// <summary>
    // /// 远端资源地址查询服务类
    // /// </summary>
    // public class RemoteServices : IRemoteServices
    // {
    //     private readonly string _defaultHostServer;
    //     private readonly string _fallbackHostServer;
    //
    //     public RemoteServices(string defaultHostServer, string fallbackHostServer)
    //     {
    //         _defaultHostServer = defaultHostServer;
    //         _fallbackHostServer = fallbackHostServer;
    //     }
    //
    //     string IRemoteServices.GetRemoteMainURL(string fileName)
    //     {
    //         return $"{_defaultHostServer}/{fileName}";
    //     }
    //
    //     string IRemoteServices.GetRemoteFallbackURL(string fileName)
    //     {
    //         return $"{_fallbackHostServer}/{fileName}";
    //     }
    // }

    public class ResourcesComponent : Singleton<ResourcesComponent>, ISingletonAwake
    {
        public void Awake()
        {
            //YooAssets.Initialize();
        }

        protected override void Destroy()
        {
            //YooAssets.Destroy();
        }

        public async Task CreatePackageAsync(string packageName, bool isDefault = false)
        {
            await Task.Delay(100);
            // ResourcePackage package = YooAssets.CreatePackage(packageName);
            // if (isDefault)
            // {
            //     YooAssets.SetDefaultPackage(package);
            // }
            //
            // GlobalConfig globalConfig = Resources.Load<GlobalConfig>("GlobalConfig");
            // EPlayMode ePlayMode = globalConfig.EPlayMode;
            //
            // // 编辑器下的模拟模式
            // switch (ePlayMode)
            // {
            //     case EPlayMode.EditorSimulateMode:
            //     {
            //         EditorSimulateModeParameters createParameters = new();
            //         createParameters.SimulateManifestFilePath = EditorSimulateModeHelper.SimulateBuild("ScriptableBuildPipeline", packageName);
            //         await package.InitializeAsync(createParameters).Task;
            //         break;
            //     }
            //     case EPlayMode.OfflinePlayMode:
            //     {
            //         OfflinePlayModeParameters createParameters = new();
            //         await package.InitializeAsync(createParameters).Task;
            //         break;
            //     }
            //     case EPlayMode.HostPlayMode:
            //     {
            //         string defaultHostServer = GetHostServerURL();
            //         string fallbackHostServer = GetHostServerURL();
            //         HostPlayModeParameters createParameters = new();
            //         createParameters.BuildinQueryServices = new GameQueryServices();
            //         createParameters.RemoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
            //         await package.InitializeAsync(createParameters).Task;
            //         break;
            //     }
            //     case EPlayMode.WebPlayMode:
            //     {
            //         string defaultHostServer = GetHostServerURL();
            //         string fallbackHostServer = GetHostServerURL();
            //         WebPlayModeParameters createParameters = new();
            //         createParameters.BuildinQueryServices = new GameQueryServices();
            //         createParameters.RemoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
            //         await package.InitializeAsync(createParameters).Task;
            //         break;
            //     }
            //     default:
            //         throw new ArgumentOutOfRangeException();
            // }
        }

        static string GetHostServerURL()
        {
            //string hostServerIP = "http://10.0.2.2"; //安卓模拟器地址
            string hostServerIP = "http://127.0.0.1";
            string appVersion = "v1.0";

#if UNITY_EDITOR
            if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.Android)
            {
                return $"{hostServerIP}/CDN/Android/{appVersion}";
            }
            else if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.iOS)
            {
                return $"{hostServerIP}/CDN/IPhone/{appVersion}";
            }
            else if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.WebGL)
            {
                return $"{hostServerIP}/CDN/WebGL/{appVersion}";
            }

            return $"{hostServerIP}/CDN/PC/{appVersion}";
#else
            if (Application.platform == RuntimePlatform.Android)
            {
                return $"{hostServerIP}/CDN/Android/{appVersion}";
            }
            else if (Application.platform == RuntimePlatform.IPhonePlayer)
            {
                return $"{hostServerIP}/CDN/IPhone/{appVersion}";
            }
            else if (Application.platform == RuntimePlatform.WebGLPlayer)
            {
                return $"{hostServerIP}/CDN/WebGL/{appVersion}";
            }

            return $"{hostServerIP}/CDN/PC/{appVersion}";
#endif
        }

        public void DestroyPackage(string packageName)
        {
            // ResourcePackage package = YooAssets.GetPackage(packageName);
            // package.UnloadUnusedAssets();
        }

        // /// <summary>
        // /// 主要用来加载dll config aotdll，因为这时候纤程还没创建，无法使用ResourcesLoaderComponent。
        // /// 游戏中的资源应该使用ResourcesLoaderComponent来加载
        // /// </summary>
        // public async ETTask<T> LoadAssetAsync<T>(string location) where T : UnityEngine.Object
        // {
        //     AssetHandle handle = YooAssets.LoadAssetAsync<T>(location);
        //     await handle.Task;
        //     T t = (T)handle.AssetObject;
        //     handle.Release();
        //     return t;
        // }
        //
        // /// <summary>
        // /// 主要用来加载dll config aotdll，因为这时候纤程还没创建，无法使用ResourcesLoaderComponent。
        // /// 游戏中的资源应该使用ResourcesLoaderComponent来加载
        // /// </summary>
        // public async ETTask<Dictionary<string, T>> LoadAllAssetsAsync<T>(string location) where T : UnityEngine.Object
        // {
        //     AllAssetsHandle allAssetsOperationHandle = YooAssets.LoadAllAssetsAsync<T>(location);
        //     await allAssetsOperationHandle.Task;
        //     Dictionary<string, T> dictionary = new Dictionary<string, T>();
        //     foreach (UnityEngine.Object assetObj in allAssetsOperationHandle.AllAssetObjects)
        //     {
        //         T t = assetObj as T;
        //         dictionary.Add(t.name, t);
        //     }
        //
        //     allAssetsOperationHandle.Release();
        //     return dictionary;
        // }
    }
}